<!DOCTYPE html><html lang="zh-Hans"><head><meta name="generator" content="Hexo 3.9.0"><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="幂等性问题"><meta name="keywords" content="幂等性"><meta name="author" content="Duingold"><meta name="copyright" content="Duingold"><title>幂等性问题 | Duingold's Blogs</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.6.1"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.6.1"><link rel="dns-prefetch" href="https://cdn.staticfile.org"><link rel="dns-prefetch" href="https://cdn.bootcss.com"><link rel="dns-prefetch" href="https://creativecommons.org"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?e8cc1b6560c9b17b5610f6381b416bbb";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  }
} </script></head><body><canvas class="fireworks"></canvas><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#原因"><span class="toc-number">1.1.</span> <span class="toc-text">原因</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#什么是幂等"><span class="toc-number">1.2.</span> <span class="toc-text">什么是幂等</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#哪些操作会出现幂等性问题"><span class="toc-number">1.3.</span> <span class="toc-text">哪些操作会出现幂等性问题</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#怎么实现幂等"><span class="toc-number">2.</span> <span class="toc-text">怎么实现幂等</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#不带唯一索引Insert"><span class="toc-number">2.1.</span> <span class="toc-text">不带唯一索引Insert:</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#计算式Update"><span class="toc-number">2.2.</span> <span class="toc-text">计算式Update:</span></a></li></ol></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://i.ibb.co/NLF3K9B/20190712022822.jpg"></div><div class="author-info__name text-center">Duingold</div><div class="author-info__description text-center">博客</div><div class="follow-button"><a href="https://github.com/Duingold">Follow Me</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">5</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">5</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">2</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(https://raw.githubusercontent.com/Duingold/imgs/master/img/hfasfjhasfklsjf123123.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">Duingold's Blogs</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus"><a class="site-page" href="/">首页</a><a class="site-page" href="/archives">归档</a><a class="site-page" href="/categories">分类</a><a class="site-page" href="/tags">标签</a></span></div><div id="post-info"><div id="post-title">幂等性问题</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2019-07-12</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/面试/">面试</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><h4 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h4><h5 id="原因"><a href="#原因" class="headerlink" title="原因"></a>原因</h5><p>在传统应用中，调用接口，只有两种状态成功和失败。但是在分布式,微服务的架构下，还有第三种情况，那就是超时！因为超时而进行重试,进行重试调用，就给系统带来了不一致问题了！因此我们必须给调用接口提供幂等性保证，防止重复调用出现不一致的情形！”</p>
<h5 id="什么是幂等"><a href="#什么是幂等" class="headerlink" title="什么是幂等"></a>什么是幂等</h5><p>幂等性强调的是外界通过接口对系统内部的影响, 外界怎么看系统和幂等性没有关系，只要一次或多次调用对某一个资源应该具有同样的副作用就行。注意了，是对资源造成的副作用必须是一样的，但是返回值允许不同！</p>
<h5 id="哪些操作会出现幂等性问题"><a href="#哪些操作会出现幂等性问题" class="headerlink" title="哪些操作会出现幂等性问题"></a>哪些操作会出现幂等性问题</h5><ul>
<li>(1)查询操作<br>查询操作并不会产生或变更新的数据，因此查询是天然具备幂等性。</li>
<li>(2)删除操作 这里分为物理删除和逻辑删除<ul>
<li>物理删除:删除只会进行一次，无论执行几次delete操作，造成的效果是一样的！是幂等性操作</li>
<li>逻辑删除:这类删除，是用update修改字段而已，这种操作无论update几次，造成的效果是一样的！是幂等性操作</li>
</ul>
</li>
<li>(3)增加操作<br>这里要看这张表是否带唯一索引。<ul>
<li>带唯一索引Insert:此时如果重复插入操作，是会插入失败的！该操作是幂等性操作</li>
<li>不带唯一索引Insert: <strong>这种情况是非幂等性操作。</strong></li>
</ul>
</li>
<li>(4)修改操作<br>要看修改了啥<ul>
<li>计算式Update:这类操作是指UPDATE table SET number=number-1 WHERE id=1，这类SQL的操作，<strong>是非幂等性操作！</strong></li>
<li>非计算式Update:这类操作是指UPDATE table SET number=3 WHERE id=1，这类SQL操作，这种修改是属于幂等性的操作！<h4 id="怎么实现幂等"><a href="#怎么实现幂等" class="headerlink" title="怎么实现幂等"></a>怎么实现幂等</h4>计算式Update和不带唯一索引Insert会出现幂等性问题<h5 id="不带唯一索引Insert"><a href="#不带唯一索引Insert" class="headerlink" title="不带唯一索引Insert:"></a>不带唯一索引Insert:</h5></li>
</ul>
</li>
</ul>
<p>添加唯一索引</p>
<p>不想建立索引</p>
<ul>
<li>修改插入语句<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> <span class="keyword">table</span>(field1, field2, fieldn) <span class="keyword">SELECT</span> <span class="string">'field1'</span>, </span><br><span class="line"><span class="string">'field2'</span>, <span class="string">'fieldn'</span> <span class="keyword">FROM</span> DUAL <span class="keyword">WHERE</span> <span class="keyword">NOT</span> <span class="keyword">EXISTS</span>(<span class="keyword">SELECT</span> field1,field2,fieldn</span><br><span class="line"> <span class="keyword">FROM</span> <span class="keyword">table</span> <span class="keyword">WHERE</span> field1 = ?)</span><br></pre></td></tr></table></figure>

</li>
</ul>
<p>采用这种写法，就可以防止重复插入，而且不需要建立唯一索引！SQL可以判断field字段有值，则不insert。如果无值，则会执行insert操作！这种方法其实就是使用了mysql的一个临时表的方式，但是里面使用到了子查询，效率也会有一点点影响。但是很重要的一点，这种写法在oracle里也能跑的通，通用性强</p>
<h5 id="计算式Update"><a href="#计算式Update" class="headerlink" title="计算式Update:"></a>计算式Update:</h5><p>增加版本号</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">UPDATE</span> <span class="keyword">table</span> <span class="keyword">SET</span> <span class="built_in">number</span>=<span class="built_in">number</span><span class="number">-1</span>，_version=_version+<span class="number">1</span>  </span><br><span class="line"><span class="keyword">WHERE</span> <span class="keyword">id</span>=<span class="number">1</span> <span class="keyword">AND</span> _version= last_version</span><br></pre></td></tr></table></figure>

<p>唯一的缺点，就是执行前，需要去数据查一下当前版本是啥！当然啦，如果你的表有唯一索引，用的又是mysql，又能保证将来不换其他数据库。可以试试mysql的on duplicate key update语句，该操作插入时如果发现unique key已存在，则执行update更新操作”</p>
<p><strong>补充:</strong><br>在数据库层面的改变是最方便的，所以我一直主张，改sql，改表结构来解决幂等性问题。不要引入一堆七七八八的东东，徒增系统复杂度。</p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Duingold</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://yoursite.com/2019/07/12/幂等性问题/">http://yoursite.com/2019/07/12/幂等性问题/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://yoursite.com">Duingold's Blogs</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/幂等性/">幂等性</a></div><div class="social-share" data-disabled="google,facebook"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js@1.0.16/dist/css/share.min.css"><script src="https://cdn.jsdelivr.net/npm/social-share.js@1.0.16/dist/js/social-share.min.js"></script><nav id="pagination"><div class="prev-post pull-left"><a href="/2019/07/12/java中的锁/"><i class="fa fa-chevron-left">  </i><span>java中的锁</span></a></div><div class="next-post pull-right"><a href="/2019/07/12/如何停止一个线程/"><span>如何停止一个线程</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer class="footer-bg" style="background-image: url(https://raw.githubusercontent.com/Duingold/imgs/master/img/hfasfjhasfklsjf123123.jpg)"><div class="layout" id="footer"><div class="copyright">&copy;2019 By Duingold</div><div class="framework-info"><span>驱动 - </span><a href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="footer_custom_text">Hi, welcome to my <a href="https://molunerfinn.com">blog</a>!</div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file-o"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.6.1"></script><script src="/js/fancybox.js?version=1.6.1"></script><script src="/js/sidebar.js?version=1.6.1"></script><script src="/js/copy.js?version=1.6.1"></script><script src="/js/fireworks.js?version=1.6.1"></script><script src="/js/transition.js?version=1.6.1"></script><script src="/js/scroll.js?version=1.6.1"></script><script src="/js/head.js?version=1.6.1"></script><script>if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
}</script><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/tororo.model.json"},"display":{"superSample":2,"width":210,"height":420,"position":"right","hOffset":0,"vOffset":-20},"log":false,"tagMode":false});</script></body></html>